package com.right.health.app.ui.household.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.magic.basic.utils.CollectionUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.BloodPressureResponse;
import com.right.health.app.net.HouseholdApi;
import com.right.health.app.ui.household.BloodPressureActivity;
import com.right.health.app.utils.LineChartUtil;
import com.right.health.app.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
 * Created by hopeliao on 2019-12-30
 *
 */
public class BloodPressureFragment1 extends BasePresenterFragment implements OnHttpListener {

    private final int[] mValues = new int[]{0, 3, 6, 9, 12, 15, 18, 21, 24};

    private LineChart mLineChart;

    private HouseholdApi mHouseholdApi;

    private BloodPressureResponse mResponse;

    private static final int X_MAX_AXIS = 24;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.blood_pressure_fragment, viewGroup, false);
    }

    @Override
    public void findView() {
        mLineChart = (LineChart) findViewById(R.id.lineChart);
        LineChartUtil.setupLineChart(mLineChart);
        LineChartUtil.getCommonXAxis(mLineChart, 9, 24, new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return value == 0 ? "" : String.format("%d:00", mValues[(int) value / 3]);
            }
        });

    }

    @Override
    public void initialize() {
        mHouseholdApi = new HouseholdApi();
        mHouseholdApi.setListener(this);

        mHouseholdApi.addRequestCode(BloodPressureActivity.HTTP_CDOE_TYPE_TYPE);
        mHouseholdApi.getBloodPressureByType(BloodPressureResponse.TYPE_DAY);
    }

    @Override
    public void refreshData() {
        super.refreshData();
        if (mHouseholdApi==null) {
            mHouseholdApi = new HouseholdApi();
            mHouseholdApi.setListener(this);
        }
        mHouseholdApi.addRequestCode(BloodPressureActivity.HTTP_CDOE_TYPE_TYPE);
        mHouseholdApi.getBloodPressureByType(BloodPressureResponse.TYPE_DAY);
    }

    private void refreshLineChat() {
        if (mResponse != null && !CollectionUtil.isEmpty(mResponse.bloodpressures)) {

            double maxValue = 0;
            List<List<Entry>> mLineValues = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                mLineValues.add(new ArrayList<>());
            }

            Collections.sort(mResponse.bloodpressures, new Comparator<BloodPressureResponse.BloodPressureInfo>() {
                @Override
                public int compare(BloodPressureResponse.BloodPressureInfo t, BloodPressureResponse.BloodPressureInfo t1) {
                    if (t1.createtime == null || t.createtime == null) {
                        return -1;
                    }
                    return (Float.valueOf(t.createtime) > Float.valueOf(t1.createtime)) ? 1 : -1;
                }
            });
            for (BloodPressureResponse.BloodPressureInfo info : mResponse.bloodpressures) {
                if (info == null || info.createtime == null || info.highpressure == null) {
                   continue;
                }
                float x = ((float)TimeUtils.getHour(info.createtime));
            /*    for (int i = 0; i < mValues.length - 1; i++) {
                    if (hour >= mValues[i] && hour <= mValues[i + 1]) {
                        float preValue = mValues[i];
                        float nextValue = mValues[i + 1];

                        float startX = i;
                        float offset = (hour - preValue) / (nextValue - preValue);

                        targetX = startX + offset;
                        break;
                    }
                }*/

                Log.e("TAG", "createtime = " + TimeUtils.getHour(info.createtime) + "，" + info.createtime);

                float highpressure = Float.valueOf(info.highpressure);
                float lowvoltage = Float.valueOf(info.lowvoltage);
                float pulse = Float.valueOf(info.pulse);

                maxValue = Math.max(maxValue, Math.ceil(highpressure));
                maxValue = Math.max(maxValue, Math.ceil(lowvoltage));
                maxValue = Math.max(maxValue, Math.ceil(pulse));

                mLineValues.get(0).add(new Entry(x, highpressure));
                mLineValues.get(1).add(new Entry(x, lowvoltage));
                mLineValues.get(2).add(new Entry(x, pulse));
            }

            LineDataSet hpLine = LineChartUtil.createLine(mLineValues.get(0), getResources().getColor(R.color.color_09C3BE));
            LineDataSet lpLine = LineChartUtil.createLine(mLineValues.get(1), getResources().getColor(R.color.color_EDAD22));
            LineDataSet plLine = LineChartUtil.createLine(mLineValues.get(2), getResources().getColor(R.color.color_229EED));
            LineChartUtil.getCommonYAxis(mLineChart, 5, (int) Math.ceil(maxValue), null);
            LineChartUtil.refresh(mLineChart, hpLine, lpLine, plLine);
        } else {
            LineChartUtil.refresh(mLineChart);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (!isAdded()) {
            return;
        }
        if (request.requestCode == BloodPressureActivity.HTTP_CDOE_TYPE_TYPE) {
            String currType = String.valueOf(request.tag);
            if (response.targetData instanceof BloodPressureResponse) {
                mResponse = response.cast(BloodPressureResponse.class);
                refreshLineChat();
            }
        }
    }


}
